{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 第24章卷积神经网络"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 习题24.1\n",
    "\n",
    "&emsp;&emsp;设有输入矩阵$X=[x_{ij}]_{I \\times J}$，核矩阵$W=[w_{mn}]_{M \\times N}$，满足$M \\ll I, N \\ll J$，则称以下运算为二维数学卷积：\n",
    "$$\n",
    "Y = W \\circledast X\n",
    "$$\n",
    "产生输出矩阵$Y=[y_{kl}]_{K \\times L}$，其中\n",
    "$$\n",
    "y_{kl} = \\sum_{m=1}^M \\sum_{n=1}^N w_{mn} x_{i-m+1, j-n+1} \\\\\n",
    "K = I - M + 1, L = J - N + 1\n",
    "$$\n",
    "证明数学卷积和机器学习卷积有以下关系：\n",
    "$$\n",
    "W \\circledast X = \\text{rot180}(W) * X\n",
    "$$\n",
    "这里$\\circledast$表示数学卷积，$*$表示机器学习卷积，rot180()表示对矩阵的180度旋转。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**\n",
    "\n",
    "1. 给出机器学习二维卷积的定义\n",
    "2. 计算$\\text{rot180}(W) * X$输出矩阵$Y^{\\prime}$\n",
    "3. 验证$Y$与$Y^{\\prime}$相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：给出机器学习二维卷积的定义**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据书中第24.1.2节的定义24.1的二维卷积的定义：\n",
    "\n",
    "> **定义24.1（二维卷积）** 给定一个 $I \\times J$ 输入矩阵 $X = [x_{i,j}]_{I \\times J}$，一个 $M \\times N$ 核矩阵 $W=  [w_{m,n}]_{M \\times N}$，满足 $M \\ll I, N \\ll J$。让核矩阵在输入矩阵上从左到右再从上到下按顺序滑动，在滑动的每一个位置，核矩阵与输入矩阵的一个子矩阵重叠。求核矩阵与每一个子矩阵的内积，产生一个 $K \\times L$ 输出矩阵 $Y= [y_{k,l}]_{K \\times L}$，称此运算为卷积（convolution）或二维卷积。写作\n",
    "> $$\n",
    "Y = W * X \\tag{24.4}\n",
    "$$\n",
    "> 其中, $Y = [y_{k,l}]_{K \\times L}$。\n",
    "> $$\n",
    "y_{k,l} = \\sum_{m=1}^M \\sum_{n=1}^N w_{m, n} x_{k+m-1, l+n-1} \\tag{24.5}\n",
    "$$\n",
    "> 其中，$k = 1, 2, \\cdots, K, \\ l = 1, 2, \\cdots, L, \\ K = I-M+1, \\ L=J-N+1$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：计算$\\text{rot180}(W) * X$输出矩阵$Y^{\\prime}$**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;令$W' = \\text{rot180}(W) = [w'_{m,n}]_{M \\times N} = [w_{M-m+1,N-n+1}]_{M \\times N}$，则对于$\\text{rot180}(W) * X$输出矩阵$Y' = [y'_{k,l}]_{K \\times L}$有："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{aligned}\n",
    "y'_{k,l}\n",
    "&= \\sum_{m=1}^M \\sum_{n=1}^{N} w'_{m,n} x_{k+m-1, l+n-1} \\\\\n",
    "&= \\sum_{m=1}^M \\sum_{n=1}^{N} w_{M-m+1,N-n+1} x_{k+m-1, l+n-1} \\\\\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;令$p=M-m+1$，且当$m = 1$时，$p = M$，当$m = M$时，$p = 1$；令$q=N-n+1$，且当$n = 1$，$q = N$，当$n = N$，$q = 1$，则："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{aligned}\n",
    "y'_{k,l} \n",
    "&= \\sum_{p=M}^1 \\sum_{q=N}^1 w_{p,q} x_{k+M-p,l+N-q} \\\\\n",
    "&= \\sum_{p=1}^M \\sum_{q=1}^N w_{p,q} x_{k+M-p,l+N-q}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第3步：验证$Y$与$Y^{\\prime}$相等**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;对于$Y = W \\circledast X=[y_{kl}]_{K \\times L}$，由题目可知：\n",
    "$$\n",
    "y_{k,l} = \\sum_{m=1}^M \\sum_{n=1}^N w_{m, n} x_{i-m+1, j-n+1} \\\\\n",
    "K = I - M + 1, L = J - N + 1\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可令$i = k + M - 1,j = l + N - 1$，则\n",
    "$$\n",
    "i - m + 1 = k + M - m \\\\\n",
    "j - n + 1 = l + N -n\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可得：\n",
    "$$\n",
    "y_{k,l} = \\sum_{m=1}^M \\sum_{n=1}^N w_{mn} x_{k + M-m, l + N-n}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;因此，$W \\circledast X = Y = Y^{\\prime} = \\text{rot180}(W) * X$，故原命题得证。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.2\n",
    "\n",
    "&emsp;&emsp;假设有矩阵\n",
    "$$\n",
    "A = \\left[ \n",
    "\\begin{array}{c}\n",
    "3 & 2 & 0 & 1 \\\\\n",
    "0 & 2 & 1 & 2 \\\\\n",
    "2 & 0 & 0 & 3 \\\\\n",
    "2 & 3 & 1 & 2\n",
    "\\end{array}\n",
    "\\right], B = \\left[ \n",
    "\\begin{array}{c}\n",
    "2 & 1 & 2 \\\\\n",
    "0 & 0 & 3 \\\\\n",
    "0 & 0 & 2\n",
    "\\end{array}\n",
    "\\right]\n",
    "$$\n",
    "其中，$A$是输入矩阵，$B$是核矩阵，可以求得数学卷积$B \\circledast A$是\n",
    "$$\n",
    "B \\circledast A = \\left[ \n",
    "\\begin{array}{c}\n",
    "6 & 7 & 8 & 6 & 1 & 3 \\\\\n",
    "0 & 4 & 13 & 15 & 4 & 7 \\\\\n",
    "4 & 2 & 10 & 16 & 6 & 14 \\\\\n",
    "4 & 8 & 15 & 15 & 6 & 17 \\\\\n",
    "0 & 0 & 10 & 9 & 3 & 12\\\\\n",
    "0 & 0 & 4 & 6 & 2 & 4\n",
    "\\end{array}\n",
    "\\right]\n",
    "$$\n",
    "试求数学卷积$A \\circledast B$，并验证数学卷积满足交换律。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**  \n",
    "\n",
    "1. 给出数学卷积的定义\n",
    "2. 给出二维数学卷积的定义\n",
    "3. 计算数学卷积$A \\circledast B$\n",
    "4. 验证数学卷积满足交换律"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：数学卷积的定义**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据书中第24.1.2节的数学卷积的定义：\n",
    "\n",
    "> &emsp;&emsp;在数学中，卷积（convolution）是定义在两个函数上的运算，表示用其中一个函数对另一个函数的形状进行的调整。这里考虑一维卷积。设 $f$ 和 $g$ 是两个可积的实值函数，则积分\n",
    "> $$\n",
    "\\int_{-\\infty}^{+\\infty} f(\\tau) g(t-\\tau) \\text{d} \\tau\n",
    "$$\n",
    "> &emsp;&emsp;定义了一个新的函数 $h (t)$，称为 $f$ 和 $g$ 的卷积，记作\n",
    "> $$\n",
    "h(t) = (f \\circledast g)(t) = \\int_{-\\infty}^{+\\infty} f(\\tau) g(t-\\tau) \\text{d} \\tau\n",
    "$$\n",
    "> 其中，符号 $\\circledast$ 表示卷积运算。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：二维数学卷积的定义**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;见习题24.1中二维数学卷积：\n",
    "$$\n",
    "Y = W \\circledast X\n",
    "$$\n",
    "其中输入矩阵$X=[x_{ij}]_{I \\times J}$，核矩阵$W=[w_{mn}]_{M \\times N}$，满足$M \\ll I, N \\ll J$，产生输出矩阵$Y=[y_{kl}]_{K \\times L}$，其中\n",
    "$$\n",
    "y_{kl} = \\sum_{m=1}^M \\sum_{n=1}^N w_{mn} x_{i-m+1, j-n+1} \\\\\n",
    "K = I - M + 1, L = J - N + 1\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第3步：计算数学卷积$A \\circledast B$**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据定义，计算$A \\circledast B$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "A \\circledast B = \\left[ \n",
    "\\begin{array}{c}\n",
    "6 & 7 & 8 & 6 & 1 & 3 \\\\\n",
    "0 & 4 & 13 & 15 & 4 & 7 \\\\\n",
    "4 & 2 & 10 & 16 & 6 & 14 \\\\\n",
    "4 & 8 & 15 & 15 & 6 & 17 \\\\\n",
    "0 & 0 & 10 & 9 & 3 & 12\\\\\n",
    "0 & 0 & 4 & 6 & 2 & 4\n",
    "\\end{array}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第4步：验证数学卷积满足交换律**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;不妨设\n",
    "$$\n",
    "h'(t)=(g \\circledast f)(t)=\\int_{-\\infty}^{+\\infty} g(\\tau) f(t-\\tau) \\mathrm{d} \\tau\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;令 $t- \\tau = \\lambda$，$\\mathrm{d} \\lambda = -\\mathrm{d}\\tau$，且当$\\tau \\rightarrow +\\infty$时，$\\lambda \\rightarrow -\\infty$；当$\\tau \\rightarrow -\\infty$时，$\\lambda \\rightarrow +\\infty$，则：\n",
    "$$\n",
    "\\begin{aligned}\n",
    "h'(t)\n",
    "&= - \\int_{+\\infty}^{-\\infty} g(t - \\lambda) f(\\lambda) \\mathrm{d} \\lambda \\\\\n",
    "&= \\int_{-\\infty}^{+\\infty} g(t - \\lambda) f(\\lambda) \\mathrm{d} \\lambda\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;因此，$(g \\circledast f)(t) = (f \\circledast g)(t)$，即数学卷积满足交换律，原命题得证。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.3\n",
    "\n",
    "&emsp;&emsp;验证机器学习卷积（互相关）不满足交换律。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**  \n",
    "\n",
    "1. 给出机器学习卷积定义\n",
    "2. 计算$X * W$\n",
    "3. 验证$X * W$与$W * X$不相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：机器学习卷积定义**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据书中第24章的本章概要的机器学习卷积的定义：\n",
    "\n",
    "> 给定输入矩阵$X = [x_{ij}]_{I \\times J}$，核矩阵$W = [w_{mn}]_{M \\times N}$。让核矩阵在输入矩阵上按顺序滑动，（二维）卷积是定义在核矩阵与输入矩阵的子矩阵的内积，产生输出矩阵$Y=[y_{kl}]_{K \\times L}$。\n",
    "$$\n",
    "Y = W * X\n",
    "$$\n",
    "其中，$Y = [y_{kl}]_{K \\times L}$，$\\displaystyle y_{kl} = \\sum_{m = 1}^M \\sum_{n = 1}^N w_{m,n} x_{k + m - 1, l + n - 1}$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：计算$X * W$**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;设$X * W$的输出矩阵为$Y' = [y'_{kl}]_{K' \\times L'}$，则\n",
    "$$\n",
    "y'_{kl} = \\sum_{i=1}^{I} \\sum_{j=1}^{J} x_{i,j} w_{k+i-1, l+j-1}\n",
    "$$\n",
    "其中$k = 1, 2, \\cdots, K', \\ l = 1, 2, \\cdots, L', \\ K'= M - I + 1, \\ L' = N - J + 1$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;令$p = k + i - 1$，当$i = 1$时，$p = k$，当$i = I$时，$p = k + I - 1$；令$q = l + j - 1$，当$j = 1$，$q = l$，当$j = J$，$q = l + J - 1$，则\n",
    "$$\n",
    "y'_{kl} = \\sum_{p = k}^{k + I - 1} \\sum_{q = l}^{l + J - 1} w_{p, q} x_{p-k+1, q-l+1}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第3步：验证$X * W$与$W * X$不相等**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;综上，$Y^{\\prime} = [y'_{kl}]_{K' \\times L'} \\neq Y =  [y_{kl}]_{K \\times L}$，即 $X * W \\neq W * X$，原命题得证。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.4\n",
    "\n",
    "&emsp;&emsp;CNN也可以用于一维数据的处理，求图24.22所示的一维卷积。\n",
    "\n",
    "![](../images/24-4.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**  \n",
    "\n",
    "1. 根据二维卷积的定义，写出一维卷积的定义\n",
    "2. 计算一维卷积的输出矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：根据二维卷积的定义，写出一维卷积的定义**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;给定一个$I \\times 1$输入矩阵$X$和$M \\times 1$核矩阵$W$，可将它们视作第2维度为1的二维矩阵，则产生的输出矩阵为\n",
    "$$\n",
    "Y = W * X = [y_{k,1}]_{K \\times 1}\n",
    "$$\n",
    "其中\n",
    "$$\n",
    "y_{k,1} = \\sum_{m=1}^M w_{m, 1} x_{k + m - 1, 1}\n",
    "$$\n",
    "其中，$k=1,2, \\cdots, K, \\ K = I - M + 1$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：计算一维卷积的输出矩阵**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据上述公式计算可得：\n",
    "\n",
    "$$\n",
    "\\begin{aligned}\n",
    "y_{1,1} &= \\sum_{m=1}^3 w_{m, 1} x_{1+m-1, 1} = 3 \\times 1 + 0 \\times -1 + 2 \\times 2 = 7 \\\\\n",
    "y_{2,1} &= \\sum_{m=1}^3 w_{m, 1} x_{2+m-1, 1} = 0 \\times 1 + 2 \\times -1 + -2 \\times 2 = -6 \\\\\n",
    "y_{3,1} &= \\sum_{m=1}^3 w_{m, 1} x_{3+m-1, 1} = 2 \\times 1 + -2 \\times -1 + 2 \\times 2 = 8 \\\\\n",
    "y_{4,1} &= \\sum_{m=1}^3 w_{m, 1} x_{4+m-1, 1} = -2 \\times 1 + 2 \\times -1 + 1 \\times 2 = -2 \\\\\n",
    "y_{5,1} &= \\sum_{m=1}^3 w_{m, 1} x_{5+m-1, 1} = 2 \\times 1 + 1 \\times -1 + 2 \\times 2 = 5\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;一维卷积的输出矩阵为\n",
    "$$\n",
    "Y = \\left[\n",
    "\\begin{array}{c}\n",
    "7 \\\\\n",
    "-6 \\\\\n",
    "8 \\\\\n",
    "-2 \\\\\n",
    "5 \n",
    "\\end{array}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.5\n",
    "\n",
    "&emsp;&emsp;通过例24.2验证卷积运算不具有旋转可变性。假设对图像数据进行90度顺时针和逆时针旋转。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**\n",
    "\n",
    "**解答思路：**  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 将输入矩阵$X$顺时针旋转90度，计算卷积核为$W$时的输出矩阵\n",
    "2. 将输入矩阵$X$逆时针旋转90度，计算卷积核为$W$时的输出矩阵\n",
    "3. 验证旋转之后产生的输出矩阵与未旋转产生的输出矩阵不相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;例24.2给定的输入矩阵$X$和核矩阵$W$：\n",
    "\n",
    "$$\n",
    "X = \\left[ \n",
    "\\begin{array}{c}\n",
    "3 & 2 & 0 & 1 \\\\\n",
    "0 & 2 & 1 & 2 \\\\\n",
    "2 & 0 & 0 & 3 \\\\\n",
    "2 & 3 & 1 & 2\n",
    "\\end{array}\n",
    "\\right], W = \\left[ \n",
    "\\begin{array}{c}\n",
    "2 & 1 & 2 \\\\\n",
    "0 & 0 & 3 \\\\\n",
    "0 & 0 & 2\n",
    "\\end{array}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：将输入矩阵$X$顺时针旋转90度，计算卷积核为$W$时的输出矩阵**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;将输入矩阵$X$顺时针旋转90度，得：\n",
    "\n",
    "$$\n",
    "X_{rot90} = \\text{rot90}(X) = \\left[ \n",
    "\\begin{array}{cccc}\n",
    "2 & 2 & 0 & 3 \\\\\n",
    "3 & 0 & 2 & 2 \\\\\n",
    "1 & 0 & 1 & 0 \\\\\n",
    "2 & 3 & 2 & 1\n",
    "\\end{array}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据二维卷积定义，得到输出矩阵\n",
    "\n",
    "$$\n",
    "Y_{rot90} = W * X_{rot90} = \\left[ \n",
    "\\begin{array}{ccc}\n",
    "2 & 1 & 2 \\\\\n",
    "0 & 0 & 3 \\\\\n",
    "0 & 0 & 2\n",
    "\\end{array}\n",
    "\\right] * \\left[ \n",
    "\\begin{array}{cccc}\n",
    "2 & 2 & 0 & 3 \\\\\n",
    "3 & 0 & 2 & 2 \\\\\n",
    "1 & 0 & 1 & 0 \\\\\n",
    "2 & 3 & 2 & 1\n",
    "\\end{array}\n",
    "\\right] = \\left[ \n",
    "\\begin{array}{cc}\n",
    "14 & 16 \\\\\n",
    "17 & 8 \n",
    "\\end{array}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：将输入矩阵$X$逆时针旋转90度，计算卷积核为$W$时的输出矩阵**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;将输入矩阵$X$逆时针旋转90度，得：\n",
    "\n",
    "$$\n",
    "X_{rot-90} = \\text{rot-90}(X) = \\left[ \n",
    "\\begin{array}{cccc}\n",
    "1 & 2 & 3 & 2 \\\\\n",
    "0 & 1 & 0 & 1 \\\\\n",
    "2 & 2 & 0 & 3 \\\\\n",
    "3 & 0 & 2 & 2\n",
    "\\end{array}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据二维卷积定义，得到输出矩阵：\n",
    "\n",
    "$$\n",
    "Y_{rot-90} = W * X_{rot-90} = \\left[ \n",
    "\\begin{array}{ccc}\n",
    "2 & 1 & 2 \\\\\n",
    "0 & 0 & 3 \\\\\n",
    "0 & 0 & 2\n",
    "\\end{array}\n",
    "\\right] * \\left[ \n",
    "\\begin{array}{ccccc}\n",
    "1 & 2 & 3 & 2 \\\\\n",
    "0 & 1 & 0 & 1 \\\\\n",
    "2 & 2 & 0 & 3 \\\\\n",
    "3 & 0 & 2 & 2\n",
    "\\end{array}\n",
    "\\right] = \\left[ \n",
    "\\begin{array}{cc}\n",
    "10 & 20 \\\\\n",
    "5 & 17 \n",
    "\\end{array}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第3步：验证旋转之后产生的输出矩阵与未旋转产生的输出矩阵不相等**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;由例24.2可知，\n",
    "$$\n",
    "Y = W * X = \\left[ \n",
    "\\begin{array}{ccc}\n",
    "2 & 1 & 2 \\\\\n",
    "0 & 0 & 3 \\\\\n",
    "0 & 0 & 2\n",
    "\\end{array}\n",
    "\\right] * \\left[ \n",
    "\\begin{array}{cccc}\n",
    "3 & 2 & 0 & 1 \\\\\n",
    "0 & 2 & 1 & 2 \\\\\n",
    "2 & 0 & 0 & 3 \\\\\n",
    "2 & 3 & 1 & 2\n",
    "\\end{array}\n",
    "\\right] = \\left[ \n",
    "\\begin{array}{cc}\n",
    "11 & 18 \\\\\n",
    "6  & 22 \n",
    "\\end{array}\n",
    "\\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;显然，$Y \\neq Y_{rot90} \\neq Y_{rot-90}$，即卷积运算不具有旋转可变性。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.6\n",
    "\n",
    "&emsp;&emsp;证明感受野的关系式（24.19）成立。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**  \n",
    "\n",
    "1. 给出感受野的关系式\n",
    "2. 写出感受野递推形式的计算公式\n",
    "3. 用数学归纳法证明感受野关系式成立"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：感受野的关系式**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据书中第24.1.4节的感受野的关系式：\n",
    "\n",
    "> &emsp;&emsp;考虑卷积神经网络全部由卷积层组成的情况，神经元的感受野的大小有以下关系成立。\n",
    "> $$\n",
    "R^{(l)} = 1 + \\sum_{j=1}^l \\left( F^{(j)} - 1 \\right) \\prod_{i=0}^{j - 1} S^{(i)} \\tag{24.19}\n",
    "$$\n",
    "> 设输入矩阵和卷积核都呈正方形。$R^{(l)} \\times R^{(l)}$ 表示第 $l$ 层的神经元的感受野的大小，$F^{(j)} \\times F^{(j)}$ 表示第 $j$ 层的卷积核的大小，$S^{(i)}$ 表示第 $i$ 层卷积的步幅，设 $S^{(0)} =1$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：写出感受野递推形式的计算公式**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;每个卷积层的每个神经元只与上一层的一部分神经元相连，故我们可根据上一层卷积层情况推导出下一层的感受野，感受野递推形式的计算公式如下：\n",
    "$$\n",
    "R^{(l)}=R^{(l-1)} + (F^{(l)} - 1) I^{(l)}\n",
    "$$\n",
    "其中，$R^{(l)} \\times R^{(l)}$和$R^{(l-1)} \\times R^{(l-1)}$分别表示第$l$和$l-1$层的神经元的感受野的大小，$F^{(j)} \\times F^{(j)}$ 表示第 $j$ 层的卷积核的大小。$I^{(l)}$表示第$l$层输出特征图的特征间的间隔，由$I^{(l)} = I^{(l-1)} \\cdot S^{l-1}$，设$I^{(0)}=1$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第3步：用数学归纳法证明感受野关系式成立**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;当$l = 1$时，$R^{1} = 1 + (F^1 - 1) = F^1$，显然成立"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;假设当 $l = n$ 时，\n",
    "$$\n",
    "R^{(n)} = 1 + \\sum_{j=1}^n \\left( F^{(j)} - 1 \\right) \\prod_{i=0}^{j-1} S^{(i)}\n",
    "$$\n",
    "&emsp;&emsp;成立。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;当$l = n+1$时，\n",
    "$$\n",
    "\\begin{aligned}\n",
    "R^{(n+1)}\n",
    "&= R^{(n)} + \\left(F^{(n+1)}-1\\right) I^{n+1} \\\\\n",
    "&= R^{(n)} + \\left(F^{(n+1)}-1\\right) I^{n}  S^{n} \\\\\n",
    "&= R^{(n)} + \\left(F^{(n+1)}-1\\right) I^{n-1}   S^{n-1} S^{n} \\\\\n",
    "&= R^{(n)} + \\left(F^{(n+1)}-1\\right) I^{0} S^{0}  S^{1} \\cdots  S^{n-1}  S^{n} \\\\\n",
    "&= R^{(n)} + \\left(F^{(n+1)}-1\\right) \\prod_{i=0}^{n} S^{(i)} \\\\\n",
    "&= 1 + \\sum_{j=1}^n \\left( F^{(j)} - 1 \\right) \\prod_{i=0}^{j-1} S^{(i)} + \\left( F^{(n+1)} - 1 \\right) \\prod_{i=0}^{n} S^{(i)} \\\\\n",
    "&= 1 + \\sum_{j=1}^{n+1} \\left( F^{(j)} - 1 \\right) \\prod_{i=0}^{j-1} S^{(i)}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结合$R^{(n)}$和$R^{(n+1)}$的关系式，可得到：\n",
    "$$\n",
    "R^{(l)} = 1 + \\sum_{j=1}^l \\left( F^{(j)} - 1 \\right) \\prod_{i=0}^{j - 1} S^{(i)}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.7\n",
    "\n",
    "&emsp;&emsp;设计一个基于CNN的自然语言句子分类模型。假设句子是单词序列，每个单词用一个实数向量表示。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**  \n",
    "\n",
    "1. 给出自然语言句子分类模型的参考文献和相关的模型架构图\n",
    "2. 使用PyTorch编程实现模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：给出自然语言句子分类模型的参考文献和相关的模型架构图**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据Chen,Yahui的[Convolutional neural network for sentence classification. MS thesis. University of Waterloo](https://arxiv.org/abs/1408.5882)论文，该论文提出了一种基于CNN的自然语言句子分类模型，其模型架构图如下："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../images/24-7.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：使用PyTorch编程实现模型**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import torch\n",
    "from torch import nn, optim\n",
    "from torch.utils.data import random_split, DataLoader\n",
    "from torchtext.data import get_tokenizer, to_map_style_dataset\n",
    "from torchtext.datasets import AG_NEWS\n",
    "from torchtext.vocab import build_vocab_from_iterator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载AG_NEWS数据集\n",
    "train_iter, test_iter = AG_NEWS(root='./data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义tokenizer\n",
    "tokenizer = get_tokenizer('basic_english')\n",
    "\n",
    "\n",
    "# 定义数据处理函数\n",
    "def yield_tokens(data_iter):\n",
    "    for _, text in data_iter:\n",
    "        yield tokenizer(text)\n",
    "\n",
    "\n",
    "# 构建词汇表\n",
    "vocab = build_vocab_from_iterator(yield_tokens(train_iter), specials=[\"<unk>\"])\n",
    "vocab.set_default_index(vocab[\"<unk>\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将数据集映射到MapStyleDataset格式\n",
    "train_dataset = list(to_map_style_dataset(train_iter))\n",
    "test_dataset = list(to_map_style_dataset(test_iter))\n",
    "# 划分验证集\n",
    "num_train = int(len(train_dataset) * 0.9)\n",
    "train_dataset, val_dataset = random_split(train_dataset, [num_train, len(train_dataset) - num_train])\n",
    "\n",
    "text_pipeline = lambda x: vocab(tokenizer(x))\n",
    "label_pipeline = lambda x: int(x) - 1\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "\n",
    "def collate_batch(batch):\n",
    "    label_list, text_list, offsets = [], [], [0]\n",
    "    for (_label, _text) in batch:\n",
    "        label_list.append(label_pipeline(_label))\n",
    "        processed_text = torch.tensor(text_pipeline(_text), dtype=torch.int64)\n",
    "        text_list.append(processed_text)\n",
    "        offsets.append(processed_text.size(0))\n",
    "    label_list = torch.tensor(label_list, dtype=torch.int64)\n",
    "    offsets = torch.tensor(offsets[:-1]).cumsum(dim=0)\n",
    "    text_list = torch.cat(text_list)\n",
    "    return label_list.to(device), text_list.to(device), offsets.to(device)\n",
    "\n",
    "\n",
    "BATCH_SIZE = 64\n",
    "# 构建数据集的数据加载器\n",
    "train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE,\n",
    "                              shuffle=True, collate_fn=collate_batch)\n",
    "valid_dataloader = DataLoader(val_dataset, batch_size=BATCH_SIZE,\n",
    "                              shuffle=True, collate_fn=collate_batch)\n",
    "test_dataloader = DataLoader(test_dataset, batch_size=BATCH_SIZE,\n",
    "                             shuffle=True, collate_fn=collate_batch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CNN_Text(nn.Module):\n",
    "    def __init__(self, vocab_size, embed_dim, class_num=4, dropout=0.5, kernel_size: list = None):\n",
    "        super(CNN_Text, self).__init__()\n",
    "        if kernel_size is None:\n",
    "            kernel_size = [3, 4, 5]\n",
    "        self.embedding = nn.EmbeddingBag(vocab_size, embed_dim, sparse=False)\n",
    "        self.convs = nn.ModuleList(\n",
    "            [nn.Conv1d(in_channels=1, out_channels=256, kernel_size=k) for k in kernel_size])\n",
    "        self.fc = nn.Sequential(\n",
    "            nn.Dropout(p=dropout),\n",
    "            nn.Linear(256 * len(kernel_size), 256),\n",
    "            nn.ReLU(),\n",
    "            nn.Dropout(p=dropout),\n",
    "            nn.Linear(256, class_num)\n",
    "        )\n",
    "\n",
    "    def forward(self, text, offsets):\n",
    "        embedded = self.embedding(text, offsets)\n",
    "        embedded = embedded.unsqueeze(0)\n",
    "        embedded = embedded.permute(1, 0, 2)\n",
    "        conv_outputs = []\n",
    "        for conv in self.convs:\n",
    "            conv_outputs.append(nn.functional.relu(conv(embedded)))\n",
    "        pooled_outputs = []\n",
    "        for conv_output in conv_outputs:\n",
    "            pooled = nn.functional.max_pool1d(conv_output, conv_output.shape[-1]).squeeze(-1)\n",
    "            pooled_outputs.append(pooled)\n",
    "        cat = torch.cat(pooled_outputs, dim=-1)\n",
    "        return self.fc(cat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置超参数\n",
    "vocab_size = len(vocab)\n",
    "embed_dim = 64\n",
    "class_num = len(set([label for label, _ in train_iter]))\n",
    "lr = 1e-3\n",
    "dropout = 0.5\n",
    "epochs = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建模型、优化器和损失函数\n",
    "model = CNN_Text(vocab_size, embed_dim, class_num, dropout).to(device)\n",
    "optimizer = optim.Adam(model.parameters(), lr=lr)\n",
    "criterion = nn.CrossEntropyLoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train(dataloader):\n",
    "    model.train()\n",
    "\n",
    "    for label, text, offsets in dataloader:\n",
    "        optimizer.zero_grad()\n",
    "        predicted_label = model(text, offsets)\n",
    "        loss = criterion(predicted_label, label)\n",
    "        loss.backward()\n",
    "        torch.nn.utils.clip_grad_norm_(model.parameters(), 0.1)\n",
    "        optimizer.step()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluate(dataloader):\n",
    "    model.eval()\n",
    "    total_acc, total_count = 0, 0\n",
    "\n",
    "    with torch.no_grad():\n",
    "        for label, text, offsets in dataloader:\n",
    "            predicted_label = model(text, offsets)\n",
    "            criterion(predicted_label, label)\n",
    "            total_acc += (predicted_label.argmax(1) == label).sum().item()\n",
    "            total_count += label.size(0)\n",
    "    return total_acc / total_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------------------------------------------------------\n",
      "| end of epoch   1 | time: 13.36s | valid accuracy     57.3% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch   2 | time: 10.13s | valid accuracy     77.8% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch   3 | time: 10.27s | valid accuracy     83.3% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch   4 | time: 10.70s | valid accuracy     85.4% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch   5 | time: 10.66s | valid accuracy     86.1% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch   6 | time: 10.63s | valid accuracy     86.5% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch   7 | time: 10.72s | valid accuracy     86.6% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch   8 | time: 10.75s | valid accuracy     86.6% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch   9 | time: 10.65s | valid accuracy     86.8% \n",
      "-----------------------------------------------------------\n",
      "-----------------------------------------------------------\n",
      "| end of epoch  10 | time: 11.39s | valid accuracy     86.5% \n",
      "-----------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "max_accu = 0\n",
    "for epoch in range(1, epochs + 1):\n",
    "    epoch_start_time = time.time()\n",
    "    train(train_dataloader)\n",
    "    accu_val = evaluate(valid_dataloader)\n",
    "    print('-' * 59)\n",
    "    print('| end of epoch {:3d} | time: {:5.2f}s | '\n",
    "          'valid accuracy {:8.1f}% '.format(epoch,\n",
    "                                            time.time() - epoch_start_time,\n",
    "                                            accu_val * 100))\n",
    "    print('-' * 59)\n",
    "    if max_accu < accu_val:\n",
    "        best_model = model\n",
    "        max_accu = accu_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test Acc: 86.9%\n"
     ]
    }
   ],
   "source": [
    "# 在测试集上测试模型\n",
    "test_acc = 0.0\n",
    "with torch.no_grad():\n",
    "    for label, text, offsets in test_dataloader:\n",
    "        output = best_model(text, offsets)\n",
    "        pred = output.argmax(dim=1)\n",
    "        test_acc += (pred == label).sum().item()\n",
    "test_acc /= len(test_dataset)\n",
    "\n",
    "print(f\"Test Acc: {test_acc * 100 :.1f}%\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "ag_news_label = {1: \"World\",\n",
    "                 2: \"Sports\",\n",
    "                 3: \"Business\",\n",
    "                 4: \"Sci/Tec\"}\n",
    "\n",
    "def predict(text, text_pipeline):\n",
    "    with torch.no_grad():\n",
    "        text = torch.tensor(text_pipeline(text))\n",
    "        output = best_model(text, torch.tensor([0]))\n",
    "        return output.argmax(1).item() + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a World news\n"
     ]
    }
   ],
   "source": [
    "ex_text_str = \"\"\"\n",
    "Our younger Fox Cubs (Y2-Y4) also had a great second experience of swimming competition in February when they travelled \n",
    "over to NIS at the end of February to compete in the SSL Development Series R2 event. For students aged 9 and under \n",
    "these SSL Development Series events are a great introduction to competitive swimming, focussed on fun and participation \n",
    "whilst also building basic skills and confidence as students build up to joining the full SSL team in Year 5 and beyond.\n",
    "\"\"\"\n",
    "best_model = best_model.to(\"cpu\")\n",
    "print(\"This is a %s news\" % ag_news_label[predict(ex_text_str, text_pipeline)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.8\n",
    "\n",
    "&emsp;&emsp;设有输入矩阵$X$和核矩阵$W$：\n",
    "$$\n",
    "X = [x_{ij}] = \\left[ \n",
    "\\begin{array}{c}\n",
    "x_{11} & x_{12} & x_{13} & x_{14} & x_{15} \\\\\n",
    "x_{21} & x_{22} & x_{23} & x_{24} & x_{25} \\\\\n",
    "x_{31} & x_{32} & x_{33} & x_{34} & x_{35} \\\\\n",
    "x_{41} & x_{42} & x_{43} & x_{44} & x_{45} \n",
    "\\end{array}\n",
    "\\right], W = [w_{mn}] = \\left[ \n",
    "\\begin{array}{c}\n",
    "w_{11} & w_{12} & w_{13} \\\\\n",
    "w_{21} & w_{22} & w_{23} \\\\\n",
    "w_{31} & w_{32} & w_{33} \n",
    "\\end{array}\n",
    "\\right]\n",
    "$$\n",
    "有卷积$Y = W * X$：\n",
    "$$\n",
    "Y = [y_{kl}] = \\left[ \n",
    "\\begin{array}{c}\n",
    "y_{11} & y_{12} & y_{13} \\\\\n",
    "y_{21} & y_{22} & y_{23} \\\\\n",
    "y_{31} & y_{32} & y_{33} \n",
    "\\end{array}\n",
    "\\right]\n",
    "$$\n",
    "求$\\displaystyle \\frac{\\partial Y}{\\partial W}$和$\\displaystyle \\frac{\\partial Y}{\\partial X}$，并具体地写出$\\displaystyle \\frac{\\text{d} y_{kl}}{\\text{d} w_{mn}}$和$\\displaystyle \\frac{\\text{d} y_{kl}}{\\text{d} x_{ij}}$。  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**  \n",
    "\n",
    "1. 给出二维卷积定义\n",
    "2. 计算 $\\displaystyle \\frac{\\partial Y}{\\partial W}$和$\\displaystyle \\frac{\\partial Y}{\\partial X}$\n",
    "3. 计算 $\\displaystyle \\frac{\\text{d} y_{kl}}{ \\text{d} w_{mn}}$和$\\displaystyle \\frac{\\text{d} y_{kl}}{\\text{d} x_{ij}}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：二维卷积定义**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据书中第24章的本章概要的二维卷积的定义：\n",
    "\n",
    "> &emsp;&emsp;给定输入矩阵$X = [x_{ij}]_{I \\times J}$，核矩阵$W = [w_{mn}]_{M \\times N}$。让核矩阵在输入矩阵上按顺序滑动，（二维）卷积是定义在核矩阵与输入矩阵的子矩阵的内积，产生输出矩阵$Y=[y_{kl}]_{K \\times L}$。\n",
    "$$\n",
    "Y = W * X\n",
    "$$\n",
    "其中，$Y = [y_{kl}]_{K \\times L}$。\n",
    "$$\n",
    "y_{kl} = \\sum_{m = 1}^M \\sum_{n = 1}^N w_{m,n} x_{k + m - 1, l + n - 1}\n",
    "$$\n",
    "其中，$k = 1, 2, \\cdots, K, l = 1,2,\\cdots, L, K = I - M + 1, L = J - N + 1$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：计算 $\\displaystyle \\frac{\\partial Y}{\\partial W}$和$\\displaystyle \\frac{\\partial Y}{\\partial X}$**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 计算 $\\displaystyle \\frac{\\partial Y}{\\partial W}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{aligned}\n",
    "\\frac{\\partial Y}{\\partial W} \n",
    "&= \\frac{\\partial Y}{\\partial w_{m,n}} \\\\\n",
    "&= \\sum_{k=1}^K \\sum_{l=1}^L \\frac{\\partial y_{kl}}{\\partial w_{m,n}} \\cdot \\frac{\\partial Y}{\\partial y_{kl} } \\\\\n",
    "&= \\sum_{k=1}^K \\sum_{l=1}^L \\frac{\\partial (w_{m,n} x_{k+m-1, l+n-1})}{\\partial w_{m,n}} \\cdot \\frac{\\partial Y}{\\partial y_{kl}} \\\\\n",
    "&= \\sum_{k=1}^K \\sum_{l=1}^L x_{k+m-1, l+n-1} \\cdot \\frac{\\partial Y}{\\partial y_{kl}}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 计算 $\\displaystyle \\frac{\\partial Y}{\\partial X}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据二维卷积的定义，可得：\n",
    "$$\n",
    "m = i - k + 1 \\\\\n",
    "n = j - l + 1 \n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "则：\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\frac{\\partial Y}{\\partial X} \n",
    "&= \\frac{\\partial Y}{\\partial x_{ij}} \\\\\n",
    "&= \\sum_{k=1}^K \\sum_{l=1}^L \\frac{\\partial y_{kl}}{\\partial x_{i j}} \\cdot \\frac{\\partial Y}{\\partial y_{kl}}\\\\\n",
    "&= \\sum_{k=1}^K \\sum_{l=1}^L \\frac{\\partial (w_{i - k + 1, j - l + 1} x_{i j})}{\\partial x_{i j}} \\cdot \\frac{\\partial Y}{\\partial y_{kl}} \\\\\n",
    "&= \\sum_{k=1}^K \\sum_{l=1}^L w_{i - k + 1, j - l + 1} \\cdot \\frac{\\partial Y}{\\partial y_{kl}}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：计算 $\\displaystyle \\frac{\\text{d} y_{kl}}{\\text{d} w_{mn}}$和$\\displaystyle \\frac{\\text{d} y_{kl}}{\\text{d} x_{ij}}$**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 计算 $\\displaystyle \\frac{\\text{d} y_{kl}}{\\text{d} w_{mn}}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\begin{aligned}\n",
    "\\frac{\\text{d} y_{kl}}{\\text{d} w_{mn}}\n",
    "&= \\frac{\\displaystyle \\text{d} \\left( \\sum_{m=1}^M \\sum_{n=1}^N {w_{m,n} x_{k+m-1, l+n-1}} \\right)}{dw_{mn}} \\\\\n",
    "&= x_{k+m-1, l+n-1}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 计算 $\\displaystyle \\frac{\\text{d} y_{kl}}{\\text{d} x_{ij}}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;首先计算 $\\displaystyle \\frac{\\text{d} y_{kl}}{\\text{d} x_{k+m-1, l+n-1}}$\n",
    "$$\n",
    "\\begin{aligned}\n",
    "\\frac{\\text{d} y_{kl}}{\\text{d} x_{k+m-1, l+n-1}}\n",
    "&= \\frac{\\displaystyle \\text{d} \\left( \\sum_{m=1}^M \\sum_{n=1}^N {w_{m,n} x_{k+m-1, l+n-1}} \\right)}{\\text{d} x_{k+m-1, l+n-1}} \\\\\n",
    "&= w_{mn}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;令$i = k + m - 1$，且当$m=1$时，$i = k$，当$m = M$时，$i = k + M -1$；令$j = l + n - 1$，且当$n=1$时，$j = l$，当$n = N$时，$j = l + N - 1$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;则当 $i \\in [k, k + M -1]$ 且 $j \\in [l, l + N -1]$时，可得：\n",
    "$$\n",
    "\\frac{\\text{d} y_{kl}}{\\text{d} x_{ij}} = w_{mn} = w_{i - k + 1, i-l+1}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;当 $i \\notin [k, k + M -1]$ 或 $j \\notin [l, l + N -1]$时，可得：\n",
    "$$\n",
    "\\frac{\\text{d} y_{kl}}{\\text{d} x_{ij}} = 0\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;综上，\n",
    "\n",
    "$$\n",
    "\\frac{\\text{d} y_{kl}}{\\text{d} x_{ij}}\n",
    "= \n",
    "\\begin{cases}\n",
    "    w_{i - k + 1, i - l + 1}, & \\text{if } i \\in [k, k + M - 1] 且 j \\in [l, l + N - 1] \\\\\n",
    "    0,              & \\text{otherwise}\n",
    "\\end{cases}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.9\n",
    "&emsp;&emsp;设有输入矩阵$X$和核矩阵$W$：\n",
    "$$\n",
    "X = \\left[ \n",
    "\\begin{array}{c}\n",
    "x_{11} & x_{12} & x_{13} \\\\\n",
    "x_{21} & x_{22} & x_{23} \\\\\n",
    "x_{31} & x_{32} & x_{33} \n",
    "\\end{array}\n",
    "\\right], W = \\left[ \n",
    "\\begin{array}{c}\n",
    "w_{11} & w_{12} & w_{13} \\\\\n",
    "w_{21} & w_{22} & w_{23} \\\\\n",
    "w_{31} & w_{32} & w_{33} \n",
    "\\end{array}\n",
    "\\right]\n",
    "$$\n",
    "验证$\\text{rot180}(W) * X = \\text{rot180}(X) * W$成立。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**  \n",
    "\n",
    "1. 给出机器学习卷积的定义\n",
    "2. 计算$\\text{rot180}(W) * X$\n",
    "3. 计算$\\text{rot180}(X) * W$\n",
    "4. 验证$\\text{rot180}(W) * X$与$\\text{rot180}(X) * W$是否相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：给出机器学习卷积的定义**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据书中第24章的本章概要的二维卷积的定义：\n",
    "\n",
    "> &emsp;&emsp;给定输入矩阵$X = [x_{ij}]_{I \\times J}$，核矩阵$W = [w_{mn}]_{M \\times N}$。让核矩阵在输入矩阵上按顺序滑动，（二维）卷积是定义在核矩阵与输入矩阵的子矩阵的内积，产生输出矩阵$Y=[y_{kl}]_{K \\times L}$。\n",
    "$$\n",
    "Y = W * X\n",
    "$$\n",
    "其中，$Y = [y_{kl}]_{K \\times L}$。\n",
    "$$\n",
    "y_{kl} = \\sum_{m = 1}^M \\sum_{n = 1}^N w_{m,n} x_{k + m - 1, l + n - 1}\n",
    "$$\n",
    "其中，$k = 1, 2, \\cdots, K, l = 1,2,\\cdots, L, K = I - M + 1, L = J - N + 1$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：计算$\\text{rot180}(W) * X$**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;由题意已知：\n",
    "$$\n",
    "\\text{rot180}(W) = [w'_{m,n}] = [w_{4-m, 4-n}]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;由机器学习卷积定义，可得：\n",
    "$$\n",
    "\\begin{aligned}\n",
    "Y'\n",
    "&= \\text{rot180}(W) * X \\\\\n",
    "&= [y'_{kl}] \\\\\n",
    "&= \\sum_{m=1}^{3} \\sum_{n=1}^{3} w'_{m, n} x_{k+m-1, l+n-1} \\\\\n",
    "&= \\sum_{m=1}^{3} \\sum_{n=1}^{3} w_{4-m, 4-n} x_{k+m-1, l+n-1}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第3步：计算$\\text{rot180}(X) * W$**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;由题意已知：\n",
    "$$\n",
    "\\text{rot180}(X) = [x'_{i,j}] = [x_{4-i, 4-j}]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;由机器学习卷积定义，可得：\n",
    "$$\n",
    "\\begin{aligned}\n",
    "Y''\n",
    "&= \\text{rot180}(X) * W \\\\\n",
    "&= [y''_{kl}] \\\\\n",
    "&= \\sum_{i=1}^{3} \\sum_{j=1}^{3} x'_{i, j} w_{k+i-1, l+j-1} \\\\\n",
    "&= \\sum_{i=1}^{3} \\sum_{j=1}^{3} x_{4-i, 4-j} w_{k+i-1, l+j-1}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第4步：验证$\\text{rot180}(W) * X$与$\\text{rot180}(X) * W$是否相等**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;由上述计算可知：\n",
    "\n",
    "$$\n",
    "Y' = \\sum_{m=1}^{3} \\sum_{n=1}^{3} w_{4-m, 4-n} x_{k+m-1, l+n-1} \\\\\n",
    "Y'' = \\sum_{i=1}^{3} \\sum_{j=1}^{3} x_{4-i, 4-j} w_{k+i-1, l+j-1} \n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;令 $4 - q = k + i - 1$，且当$i = 1$时，$q = 4 - k$，当$i = 3$时，$q  = 2 - k$；\n",
    "\n",
    "&emsp;&emsp;又令 $4 - p = l + j - 1$，且当$j = 1$时，$p = 4 - l$，当$j = 3$时，$p  = 2 - l$，  \n",
    "\n",
    "&emsp;&emsp;则 \n",
    "$$\n",
    "i = 5 - q - k \\\\ \n",
    "j = 5 - p - l\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;因此\n",
    "$$\n",
    "\\begin{aligned}\n",
    "Y'' \n",
    "&= [y''_{k,l}] \\\\\n",
    "&= \\sum_{i=1}^{3} \\sum_{j=1}^{3} x_{4-i, 4-j} w_{k+i-1, l+j-1} \\\\\n",
    "&= \\sum_{q = 4 - k}^{2 - k} \\sum_{p = 4 - l}^{2 - l} w_{4-q, 4-p} x_{k + q - 1, l + p - 1}\n",
    "\\end{aligned}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;综上，$Y' = Y''$，即 $\\text{rot180}(W) * X = \\text{rot180}(X) * W $，原命题得证。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 习题24.10\n",
    "&emsp;&emsp;解释残差网络为什么能防止梯度消失和梯度爆炸。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答思路：**  \n",
    "\n",
    "1. 给出残差网络的基本概念\n",
    "1. 描述梯度消失和梯度爆炸的现象\n",
    "1. 解释残差网络能防止梯度消失和梯度爆炸的原因"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解答步骤：**   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第1步：残差网络的基本概念**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据书中第24章的本章概要的残差网络的描述：\n",
    "\n",
    "> &emsp;&emsp;残差网络是为了解决深度神经网络训练困难而提出的深度学习方法。假设要学习的真实模型是函数 $h (x)$，也可以写作\n",
    ">$$\n",
    "h(x) = x + (h(x) - x)\n",
    "$$\n",
    "> &emsp;&emsp;残差网络的想法是用一个神经网络 $f(x)$ 近似残差 $h(x) - x$，用 $x + f(x)$ 近似真实模型 $h(x)$，整个过程以递归的方式进行。\n",
    "> $$\n",
    "x_i=x_i-1+f_i (x_i-1), i=1,2, \\cdots, n\n",
    "$$\n",
    "> &emsp;&emsp;残差网络由很多个残差单元串联连接组成。每一个残差单元相当于一般的前馈网络的两层，每一层由线性变换和非线性变换组成，还有一个残差连接。  \n",
    "> &emsp;&emsp;残差单元的输入是向量 $x$，输出是向量 $y$ 时，整个单元的运算是\n",
    "> $$\n",
    "f(x) = W_2 \\text{relu} (W_1 x) \\\\\n",
    "y = \\text{relu} (x + f(x))\n",
    "$$\n",
    "> &emsp;&emsp;残差网络可以展开成多个神经网络模块的集成，有很强的表示和学习能力。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第2步：梯度消失和梯度爆炸的现象**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;根据书中第23.2.5节的梯度消失与梯度爆炸：\n",
    "\n",
    "> &emsp;&emsp;深度神经网络学习中有时会出现梯度消失(vanishing gradient)或者梯度爆炸(exploding gradient)现象。使用反向传播算法时，首先通过正向传播计算神经网络各层的输出，然后通过反向传播神经网络各层的误差以及梯度，接着利用梯度下降公式对神经网络各层的参数进行更新。在这个过程中，各层的梯度，特别是前面层的梯度，有时会接近0（梯度消失）或接近无穷（梯度爆炸）。梯度消失会导致参数更新停止，梯度爆炸会导致参数溢出，都会使学习无法有效地进行。\n",
    "> 反向传播中，首先计算误差向量：\n",
    "> $$\n",
    "\\delta^{(t - 1)} = \\left \\{ \\text{diag}\\left( \\frac{\\partial a}{\\partial z^{(t - 1)}} \\right) \\cdot {W^{(t)}}^T \\right \\} \\cdot \\delta^{(t)} \\tag{23.57}\n",
    "$$\n",
    "> 之后计算梯度：\n",
    "> $$\n",
    "\\left\\{ \n",
    "\\begin{array}{l}\n",
    "\\nabla_{W^{(t)}} L = \\delta^{(t)} \\cdot {h^{(t - 1)}}^T \\\\\n",
    "\\nabla_{b^{(t)}} L = \\delta^{(t)}\n",
    "\\end{array}\n",
    "\\right . \\tag{23.58}\n",
    "$$\n",
    "> 造成梯度消失和梯度爆炸的原因有两种：\n",
    "> 1. 每一层的误差向量实际由矩阵的连乘决定，连乘得到的矩阵的元素可能会接近0，也可能会接近无穷，导致梯度的元素也会接近0或接近无穷，而且越是前面的层，这个问题就越严重。\n",
    "> 2. 得到每一层的误差向量之前，每个元素乘以激活函数的导数，如果激活函数的导数过小，也容易引起梯度消失，而且越是前面的层，这个问题就越严重。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;假设 $L$ 表示神经网络的层数，$w_{i,j}^{(l)}$ 表示第 $l$ 层中第 $i$ 个神经元到第 $l+1$ 层中第 $j$ 个神经元的权重，$a_i^{(l)}$ 表示第 $l$ 层中第 $i$ 个神经元的激活值，$z_i^{(l)}$ 表示第 $l$ 层中第 $i$ 个神经元的加权输入，$g(z)$ 表示激活函数，$y$ 表示网络的输出值，$y_i$ 表示输出向量的第 $i$ 个元素。则网络的输出可以表示为："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "y=g\\left(z^{(L)}\\right)=g\\left(\\boldsymbol{w}^{(L)} g\\left(\\boldsymbol{w}^{(L-1)} g\\left(\\cdots g\\left(\\boldsymbol{w}^{(1)} \\boldsymbol{x}\\right)\\right)\\right)\\right)\n",
    "$$\n",
    "其中 $\\boldsymbol{x}$表示输入变量。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;对于一个损失函数 $J(y, \\hat{y})$，网络的参数 $\\boldsymbol{w}$ 可以通过梯度下降法来更新：\n",
    "\n",
    "$$\n",
    "\\boldsymbol{w}_{t+1}=\\boldsymbol{w}_t-\\eta \\frac{\\partial J(y, \\hat{y})}{\\partial \\boldsymbol{w}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;在反向传播过程中，可以通过链式法则来计算每个神经元的梯度。具体地，对于第 $l$ 层中第 $i$ 个神经元的梯度，可以表示为：\n",
    "\n",
    "$$\n",
    "\\frac{\\partial J}{\\partial z_i^{(l)}}=\\sum_{j=1}^{m^{(l+1)}} \\frac{\\partial J}{\\partial z_j^{(l+1)}} \\frac{\\partial z_j^{(l+1)}}{\\partial z_i^{(l)}}\n",
    "$$\n",
    "其中 $m^{(l+1)}$ 表示第 $l+1$ 层的神经元数量。然后，可以使用梯度下降法来更新参数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;因为梯度是从输出层向输入层反向传播的，则梯度的大小可以表示为：\n",
    "\n",
    "$$\n",
    "\\left|\\frac{\\partial J}{\\partial z_i^{(l)}}\\right|=\\left|\\sum_{j=1}^{m^{(l+1)}} \\frac{\\partial J}{\\partial z_j^{(l+1)}} \\frac{\\partial z_j^{(l+1)}}{\\partial z_i^{(l)}}\\right| .\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;由于 $z_j^{(l+1)}$ 依赖于 $z_i^{(l)}$，因此 $\\frac{\\partial z_j^{(l+1)}}{\\partial z_i^{(l)}}$ 可能会接近0或接近无穷，导致梯度爆炸或梯度消失的情况。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;如果 $\\displaystyle \\left| \\frac{\\partial z_j^{(l+1)}}{\\partial z_i^{(l)}} \\right| > 1 $，那么在反向传播时，梯度会呈现指数级增长，导致梯度爆炸的问题。这种情况通常会导致权重的值变得非常大，从而使模型无法收敛。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;相反，如果 $\\displaystyle \\left|\\frac{\\partial z_j^{(l+1)}}{\\partial z_i^{(l)}}\\right| < 1$，那么在反向传播时梯度会逐渐变小，导致梯度消失的问题。这种情况通常会导致网络无法学习到深层特征，从而导致模型性能下降。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**第3步：解释残差网络能防止梯度消失和梯度爆炸的原因**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;在残差网络中，每个残差单元可以表示为 $y = f(x) + x$，其中 $x$ 表示输入，$f(x)$ 表示卷积层的输出，$y$ 表示残差单元的输出。从数学的角度来看，这个残差单元可以被看作是一个恒等映射加上一个残差映射，即 $y=x+\\Delta(x)$，其中 $\\Delta(x) = f(x)-x$ 表示残差。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;简单地，考虑这个残差单元的导数。计算 $y$ 对 $x$ 的导数有：\n",
    "\n",
    "$$\n",
    "\\frac{\\partial y}{\\partial x}=\\frac{\\partial y}{\\partial f(x)} \\frac{\\partial f(x)}{\\partial x}+\\frac{\\partial y}{\\partial x}=\\frac{\\partial f(x)}{\\partial x}+1\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;残差网络能防止梯度消失和梯度爆炸的原因如下：\n",
    "\n",
    "1. 由于残差网络中的 $\\displaystyle \\frac{\\partial f(x)}{\\partial x}$不可能一直为-1，因此在残差网络中不容易出现梯度消失的问题。\n",
    "\n",
    "2. 由于残差网络中的残差映射是通过将输入直接加到输出上实现的，因此它不会影响导数的大小。这意味着如果 $\\displaystyle \\frac{\\partial y}{\\partial x}$ 接近于 1，那么 $\\displaystyle \\frac{\\partial J}{\\partial x}$（$J$ 表示损失函数）的值也会比较稳定，不会出现梯度消失或梯度爆炸的问题。\n",
    "\n",
    "3. 由于残差映射中的 $\\Delta(x)$ 只包含了局部信息，而没有涉及到全局信息，因此它通常不会影响梯度的大小。这意味着即使 $\\displaystyle \\frac{\\partial y}{\\partial x}$ 很小，梯度也可以通过残差映射传递到后面的层中，从而避免了梯度消失的问题。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参考文献"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【1】Yoon Kim. 2014. Convolutional Neural Networks for Sentence Classification[J]. cs.CL, abs/1408.5882"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "273.188px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  },
  "vscode": {
   "interpreter": {
    "hash": "db1b789d0cbc5ff53f4c6976c16efd63cb2436a6fe30393f5062e1f13075a381"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
